package main

import (
	pb "gitee.com/micro-services/protocol/hotel/server"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/registry/etcdv3/v2"
	"github.com/opentracing/opentracing-go"
	"log"
	"micro.hotel.server/ao"
	"micro.hotel.server/config"
	"micro.hotel.server/database"
	"micro.hotel.server/plugins"
	"time"
)

func main() {
	tracer, c, err := plugins.NewJaegerTracer("go.micro.user.server", "localhost:6831")
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()
	opentracing.SetGlobalTracer(tracer)

	reg := etcdv3.NewRegistry(func(opts *registry.Options) {
		// etcd 集群地址
		opts.Addrs = config.Endpoint
	})

	service := micro.NewService(
		micro.Name("go.micro.hotel.server"),
		micro.Registry(reg),
		micro.RegisterTTL(time.Second*30),      // 服务注册TTL生存期
		micro.RegisterInterval(time.Second*10), // N秒重新注册一次
		//micro.WrapHandler(ocplugin.NewHandlerWrapper(tracer)),
	)

	err = config.InitCfg("/micro/database", config.AppConf)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("GConf:%+v", config.GConf)

	database.Init()
	database.GetDB().ShowSQL(true)
	defer database.CloseDB()

	service.Init()

	err = pb.RegisterHotelServerHandler(service.Server(), new(ao.Hotel))
	if err != nil {
		log.Fatal(err)
	}

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
